﻿using HalconDotNet;
using IniReadWrite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace VisionRoc.Core
{
    /// <summary>
    /// 上相机9点标定
    /// </summary>
    public class NineCalibrationt
    {
      public  CalibrationData m_CalibrationData;

        /// <summary>
        /// 9点标定INI文件参数调试路径
        /// </summary>
        public string Path;

        /// <summary>
        /// 9点标定文件路径
        /// </summary>
        public string NineCalibrationFilePath;

        public NineCalibrationt(string _Path)
        {
            try
            {
                int state = 0;
                m_CalibrationData = new CalibrationData();
                //判断文件路径是否存在
                if (!Directory.Exists(_Path))
                {
                    Directory.CreateDirectory(_Path);
                }
                Path = _Path + "\\RotateParameter.ini";
                if (File.Exists(Path))
                {
                    ReadParameter();
                    state += 1;
                }

                //判断标定文件是存在
                if (File.Exists(NineCalibrationFilePath))
                {
                    ReafCalibrationXML(NineCalibrationFilePath);
                    //HomMat2D = RotateCalculateAlgo.GenHomMat2D(m_CalibrationData.SX, m_CalibrationData.SY, m_CalibrationData.Theta, m_CalibrationData.Phi, m_CalibrationData.TX, m_CalibrationData.TY);
                    state += 1;
                }

                if (state == 2)
                {
                    InIState = 1;
                }
                else
                {
                    InIState = -1;
                }

            }
            catch
            {
                InIState = -1;
            }
        }
        /// <summary>
        /// 初始化状态  1为初始化成功  -1为初始化失败 
        /// </summary>
        public int InIState;

        public HTuple HomMat2D = new HTuple();

        public double ModelX;

        public double ModelY;

        public double ModelR;

        public void ReafCalibrationXML(string Path)
        {
            using (FileStream fs = new FileStream(Path, FileMode.Open))
            {
                XmlSerializer xml = new XmlSerializer(typeof(CalibrationData));
                m_CalibrationData = xml.Deserialize(fs) as CalibrationData;
                fs.Close();
            }
        }


        /// <summary>
        /// 保存旋转纠偏参数
        /// </summary>
        public void WriteParameter()
        {
            IniAPI.INIWriteValue(Path, "Parameter", "ModelX", ModelX.ToString());
            IniAPI.INIWriteValue(Path, "Parameter", "ModelY", ModelY.ToString());
            IniAPI.INIWriteValue(Path, "Parameter", "ModelAngle", ModelR.ToString());
            IniAPI.INIWriteValue(Path, "Parameter", "NineCalibrationFilePath", NineCalibrationFilePath.ToString());
        }

        /// <summary>
        /// 读取旋转纠偏参数
        /// </summary>
        public void ReadParameter()
        {
            ModelX = Convert.ToDouble(IniAPI.INIGetStringValue(Path, "Parameter", "ModelX", "0"));
            ModelY = Convert.ToDouble(IniAPI.INIGetStringValue(Path, "Parameter", "ModelY", "0"));
            ModelR = Convert.ToDouble(IniAPI.INIGetStringValue(Path, "Parameter", "ModelAngle", "0"));
            NineCalibrationFilePath = IniAPI.INIGetStringValue(Path, "Parameter", "NineCalibrationFilePath", "0");
        }
    }
}
